package com.example.demo.service;

import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class UserStatusService {

    @Resource
    private RedisTemplate<String, String> StringredisTemplate;

    private final static String ONLINE_USERS= "online_users";

    public void userOnline(Integer userId){
        StringredisTemplate.opsForSet().add(ONLINE_USERS, String.valueOf(userId));
    }

    public void userOffline(Integer userId){
        StringredisTemplate.opsForSet().remove(ONLINE_USERS, String.valueOf(userId));
    }

    public Boolean isUserOnline(Integer userId){
        return StringredisTemplate.opsForSet().isMember(ONLINE_USERS, String.valueOf(userId));
    }
}
